from random import Random
from Online.settings import EMAIL_FROM
from users.models import EmailVerifyRecord
from django.core.mail import send_mail
def generate_random_str(randomlength=8):
    str = ''
    chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
    length = len(chars)-1
    random = Random()
    for i in range(randomlength):
        str+=chars[random.randint(0,length)]
    return str

def send_register_email(email,send_type='register'):
    email_record = EmailVerifyRecord()
    if send_type == 'update_email':
        code = generate_random_str(4)
    else:
        code = generate_random_str(16)
    email_record.code = code
    email_record.email = email
    email_record.send_type = send_type
    email_record.save()

    email_title = ''
    email_body = ''

    if send_type == 'register':
        email_title = '爱你科在线教育账号激活服务'
        email_body = '\t您好！这是爱你科在线教育平台注册邮箱认证服务，请点击下面的链接激活您的账号：\n\t\t\t http://127.0.0.1:8000/active/{0} \n\t爱你科在线教育祝您学习生活愉快！谢谢！'.format(code)
        send_status = send_mail(email_title,email_body,EMAIL_FROM,[email])
        if send_status:
            pass
    elif send_type == 'forget':
        email_title = '爱你科在线教育账号重置密码服务'
        email_body = '\t您好！这是爱你科在线教育平台重置密码服务，请点击下面的链接重置密码：\n\t\t\t http://127.0.0.1:8000/reset/{0} \n\t爱你科在线教育祝您学习生活愉快！谢谢！'.format(code)
        send_status = send_mail(email_title,email_body,EMAIL_FROM,[email])
        if send_status:
            pass
    elif send_type == 'update_email':
        email_title = '爱你科在线教育修改邮箱服务'
        email_body = '\t您好！这是爱你科在线教育平台修改邮箱服务，请填充验证码进行修改邮箱：{0} \n\t爱你科在线教育祝您学习生活愉快！谢谢！'.format(code)
        send_status = send_mail(email_title,email_body,EMAIL_FROM,[email])
        if send_status:
            pass


